package stack;

import java.util.Stack;

/**
 * @author : Rookie
 * @description : 有效的括号
 * @date : 2023/11/5
 */
public class EffectiveBracket {

    public static void main(String[] args) {
        String input = "([{()]}}";
        String input2 = "{[()]}";
        System.out.println(isEffectiveBracket(input));
        System.out.println(isEffectiveBracket(input2));
    }

    private static boolean isEffectiveBracket(String str) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '(') {
                stack.push(')');
            } else if (c == '[') {
                stack.push(']');
            } else if (c == '{') {
                stack.push('}');
            } else if (stack.isEmpty() || stack.peek() != c) {
                return false;
            } else {
                stack.pop();
            }
        }
        return stack.isEmpty();
    }
}
